package pl.ptemich.bumbastic;




public class PointyOrientationHex extends AbstractHex {

    // - the / 
    double a1Factor;

    // - the \ 
    double a2Factor;
    
    //top left wall
    double b1tFactor;
    
    //bottom right wall
    double b1bFactor;
    
    //bottom left wall
    double b2tFactor;
    
    //top right wall
    double b2bFactor;    
    
    double circle;
    
    public PointyOrientationHex(int side, double x, double y, int hexX, int hexY) {
        this(side, Double.valueOf(x).intValue(), Double.valueOf(y).intValue(), hexX, hexY);
    }
    
    public PointyOrientationHex(int side, int posX, int posY, int hexX, int hexY) {
        this.side = side;
        this.center = new Point(posX, posY);
        
        this.hexX = hexX;
        this.hexY = hexY;
        
        double h = PointyOrientationHex.calculateH(side);
        double r = PointyOrientationHex.calculateR(side);
        
        circle = Math.sqrt((posX * posX) + (posY * posY));
        
        vertexes = new Point[6];
        vertexes[0] = new Point(posX - r, posY - (side / 2));
        vertexes[1] = new Point(posX, posY - (side / 2) - h);
        vertexes[2] = new Point(posX + r, posY - (side / 2));                
        vertexes[3] = new Point(posX + r, posY + (side / 2));        
        vertexes[4] = new Point(posX, posY + (side / 2) + h);
        vertexes[5] = new Point(posX - r, posY + (side / 2));
        
        //a = (y1 - y2) / (x1 + x2)
        a1Factor = (vertexes[0].getdoublePosY() - vertexes[1].getdoublePosY()) / (vertexes[0].getdoublePosX() - vertexes[1].getdoublePosX());
        a2Factor = (vertexes[1].getdoublePosY() - vertexes[2].getdoublePosY()) / (vertexes[1].getdoublePosX() - vertexes[2].getdoublePosX());
       
        //b = y2 - a * x2
        b1tFactor = vertexes[1].getdoublePosY() - a1Factor * vertexes[1].getdoublePosX();
        b1bFactor = vertexes[4].getdoublePosY() - a1Factor * vertexes[4].getdoublePosX();
        b2tFactor = vertexes[2].getdoublePosY() - a2Factor * vertexes[2].getdoublePosX();
        b2bFactor = vertexes[5].getdoublePosY() - a2Factor * vertexes[5].getdoublePosX();        
    }
    

    public double getCircle() {
        return circle;
    }

    public boolean checkSelected(int mouseX, int mouseY) {
        selected = false;
        double r = PointyOrientationHex.calculateR(side);
        if (mouseX > (center.getPosX() - r) && mouseX < (center.getPosX() + r)) {
            //reversed screen coordinates !!! Y rises to the bottom
            if (mouseY - 1 > a1Factor * mouseX + b1tFactor) {  //top left 
                if (mouseY + 1 < a1Factor * mouseX + b1bFactor) { //bottom right
                    if (mouseY - 1> a2Factor * mouseX + b2tFactor) {  //top right
                        if (mouseY + 1 < a2Factor * mouseX + b2bFactor) {  //bottom left
                            selected = true;
                        }
                    }
                }    
            }
        }
        return selected;
    }
 
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("HEX:");
        sb.append(center.getPosX()).append("-").append(center.getPosY())
        .append(", ").append(hexX).append("-").append(hexY).append("\n")
        .append("a:").append(a1Factor).append(" b1:").append(b1tFactor).append(" b2:").append(b1bFactor).append("\n")
        .append("a:").append(a2Factor).append(" b1:").append(b2tFactor).append(" b2:").append(b2bFactor);
        return sb.toString();
    }

    
}
